/* ---------------------------------------------------------------------- */
/* Script generated with: DeZign for Databases v4.1.3                     */
/* Target DBMS:           MS SQL Server 2005                              */
/* Project file:          bug.dez                                         */
/* Project name:                                                          */
/* Author:                                                                */
/* Script type:           Database creation script                        */
/* Created on:            2012-03-09 09:03                                */
/* ---------------------------------------------------------------------- */


/* ---------------------------------------------------------------------- */
/* Tables                                                                 */
/* ---------------------------------------------------------------------- */

/* ---------------------------------------------------------------------- */
/* Add table "NhanVien"                                                   */
/* ---------------------------------------------------------------------- */

CREATE TABLE [NhanVien] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_Dang_Nhap] NVARCHAR(40),
    [Ma_Vai_Tro] INTEGER,
    [Ghi_Chu] NVARCHAR(40),
    [Dia_Chi] NVARCHAR(40),
    [Gioi_Tinh] NVARCHAR(40),
    [So_Dien_Thoai] NVARCHAR(40),
    CONSTRAINT [PK_NhanVien] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "VaiTro"                                                     */
/* ---------------------------------------------------------------------- */

CREATE TABLE [VaiTro] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_Vai_Tro] NVARCHAR(40),
    [Ghi_Chu] NVARCHAR(40),
    CONSTRAINT [PK_VaiTro] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "ChucNang"                                                   */
/* ---------------------------------------------------------------------- */

CREATE TABLE [ChucNang] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_Chuc_Nang] NVARCHAR(40),
    [Ghi_Chu] VARCHAR(40),
    CONSTRAINT [PK_ChucNang] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "PhanQuyen"                                                  */
/* ---------------------------------------------------------------------- */

CREATE TABLE [PhanQuyen] (
    [Ma_Vai_Tro] INTEGER NOT NULL,
    [Ma_Chuc_Nang] INTEGER NOT NULL,
    CONSTRAINT [PK_PhanQuyen] PRIMARY KEY ([Ma_Vai_Tro], [Ma_Chuc_Nang])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "HoaDon"                                                     */
/* ---------------------------------------------------------------------- */

CREATE TABLE [HoaDon] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ngay_Lap] NVARCHAR(40),
    [Ghi_Chu] NVARCHAR(40),
    [Ma_Nhan_Vien] INTEGER,
    [Ma_khach_hang] INTEGER,
    CONSTRAINT [PK_HoaDon] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "SanPham"                                                    */
/* ---------------------------------------------------------------------- */

CREATE TABLE [SanPham] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_San_Pham] NVARCHAR(40),
    [Ghi_Chu] NVARCHAR(40),
    [Gia tien] NUMERIC,
    CONSTRAINT [PK_SanPham] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "LoiSanPham"                                                 */
/* ---------------------------------------------------------------------- */

CREATE TABLE [LoiSanPham] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ma_kh] INTEGER,
    [ngay_nhan] VARCHAR(40),
    [ngay_hoan_tat] VARCHAR(40),
    [ghi_chu] VARCHAR(40),
    CONSTRAINT [PK_LoiSanPham] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "KhachHang"                                                  */
/* ---------------------------------------------------------------------- */

CREATE TABLE [KhachHang] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_Khach_Hang] NVARCHAR(40),
    [Ma_San_Pham] NVARCHAR(40),
    [Ghi_Chu] NVARCHAR(40),
    [Dia_Chi] NVARCHAR(40),
    [So_Dien_Thoai] NVARCHAR(40),
    CONSTRAINT [PK_KhachHang] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "ChiTietHoaDon"                                              */
/* ---------------------------------------------------------------------- */

CREATE TABLE [ChiTietHoaDon] (
    [ID] INTEGER IDENTITY(0,1) NOT NULL,
    [Ten_San_Pham] NVARCHAR(40),
    [Ten_Khach_Hang] NVARCHAR(40),
    [Ma_San_Pham] INTEGER,
    [So_luong] INTEGER,
    [Ghi_chu] VARCHAR(40),
    [M_hoa_don] INTEGER,
    CONSTRAINT [PK_ChiTietHoaDon] PRIMARY KEY ([ID])
)
GO

/* ---------------------------------------------------------------------- */
/* Add table "Chi_tiet_loi"                                               */
/* ---------------------------------------------------------------------- */

CREATE TABLE [Chi_tiet_loi] (
    [Id] INTEGER NOT NULL,
    [Mo-ta] VARCHAR(40),
    [Yeu_cau_cua_kh] VARCHAR(40),
    [Ma_hoa_don] INTEGER,
    [Ma_loi] INTEGER,
    [masp] INTEGER,
    CONSTRAINT [PK_Chi_tiet_loi] PRIMARY KEY ([Id])
)
GO

/* ---------------------------------------------------------------------- */
/* Foreign key constraints                                                */
/* ---------------------------------------------------------------------- */

ALTER TABLE [NhanVien] ADD CONSTRAINT [VaiTro_NhanVien] 
    FOREIGN KEY ([Ma_Vai_Tro]) REFERENCES [VaiTro] ([ID])
GO

ALTER TABLE [PhanQuyen] ADD CONSTRAINT [VaiTro_PhanQuyen] 
    FOREIGN KEY ([Ma_Vai_Tro]) REFERENCES [VaiTro] ([ID])
GO

ALTER TABLE [PhanQuyen] ADD CONSTRAINT [ChucNang_PhanQuyen] 
    FOREIGN KEY ([Ma_Chuc_Nang]) REFERENCES [ChucNang] ([ID])
GO

ALTER TABLE [HoaDon] ADD CONSTRAINT [NhanVien_HoaDon] 
    FOREIGN KEY ([Ma_Nhan_Vien]) REFERENCES [NhanVien] ([ID])
GO

ALTER TABLE [HoaDon] ADD CONSTRAINT [KhachHang_HoaDon] 
    FOREIGN KEY ([Ma_khach_hang]) REFERENCES [KhachHang] ([ID])
GO

ALTER TABLE [LoiSanPham] ADD CONSTRAINT [KhachHang_LoiSanPham] 
    FOREIGN KEY ([Ma_kh]) REFERENCES [KhachHang] ([ID])
GO

ALTER TABLE [ChiTietHoaDon] ADD CONSTRAINT [SanPham_ChiTietHoaDon] 
    FOREIGN KEY ([Ma_San_Pham]) REFERENCES [SanPham] ([ID])
GO

ALTER TABLE [ChiTietHoaDon] ADD CONSTRAINT [HoaDon_ChiTietHoaDon] 
    FOREIGN KEY ([M_hoa_don]) REFERENCES [HoaDon] ([ID])
GO

ALTER TABLE [Chi_tiet_loi] ADD CONSTRAINT [HoaDon_Chi_tiet_loi] 
    FOREIGN KEY ([Ma_hoa_don]) REFERENCES [HoaDon] ([ID])
GO

ALTER TABLE [Chi_tiet_loi] ADD CONSTRAINT [LoiSanPham_Chi_tiet_loi] 
    FOREIGN KEY ([Ma_loi]) REFERENCES [LoiSanPham] ([ID])
GO

ALTER TABLE [Chi_tiet_loi] ADD CONSTRAINT [SanPham_Chi_tiet_loi] 
    FOREIGN KEY ([masp]) REFERENCES [SanPham] ([ID])
GO
